package com.wjs.gulimall.gulimallorder.listener;

import com.rabbitmq.client.Channel;
import com.wjs.gulimall.gulimallorder.entity.OrderEntity;
import com.wjs.gulimall.gulimallorder.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        System.out.println("收到过期的订单信息，准备关闭订单" + orderEntity.getOrderSn());
        // 确认接收到消息，不批量接收
        try {
            orderService.closeOrder(orderEntity);
            //一切正常就ack消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //捕获到异常就进行拒绝重新入队列的操作
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
